<template>
	<view style="margin-top:15px;">
		<managerCom></managerCom>
	</view>
</template>

<script>
import managerCom from "@/pages/manager/managerCom/managerCom.vue";
import {
	authorizeByPwd,
	accessToken,
	fetchUserInfo,
	fetchLoginState,
	fetchCompanyList,
	loginCompany,
	regByValidCode
} from "@/apis/user.js";
import {
	getZoneInfo,
	sendVerifyCode
} from "@/apis/common.js";

export default {
	components: {
		managerCom
	},
	data() {
		return {

		};
	},
	onLoad() {
		console.log('getQueryVariable',this.$route.query)
		const currAccountInfo = uni.getStorageSync('accountInfo') || {}
		const query =this.$route.query
		if (query.AccessToken && query.AccessToken != currAccountInfo.AccessToken){
			this.getUserByAccessToken(query.AccessToken)
		}
	},
	methods: {
		async getUserByAccessToken(AccessToken){
			const CmpyCatgMap = {
				0: 'Normal',
				1: 'QCJY',
				2: 'SJWX',
				3: 'JD',
				4: 'DNWX',
				5: 'JDWX',
				6: 'JZBJ',
				7: 'CDZ',
				8: 'School',
				101: 'GZYWDC'
			}
			const accountInfo = {}
			accountInfo.accessToken = AccessToken
			uni.setStorageSync('accountInfo', accountInfo);

			let userInfoRes = await fetchUserInfo({
				AccountId: accountInfo.accountId,
				lang: 'zh_CN'
			})
			if (userInfoRes.MsgCode != 0) return
			accountInfo.userInfo = userInfoRes
			accountInfo.accountId = userInfoRes.AccountID

			let loginRes = await fetchLoginState()
			if (loginRes.MsgCode != 0) return

			accountInfo.loginState = loginRes.data
			const cmpy = {}
			cmpy.AccountId = loginRes.data.help10000_passport_account_id
			cmpy.AppId = loginRes.data.help10000_passport_weixin_appid
			cmpy.AppName = loginRes.data.help10000_passport_weixin_appname
			cmpy.CmpyCatg = loginRes.data.help10000_current_company_catg
			cmpy.CmpyCode = loginRes.data.help10000_company_code
			cmpy.EmployeeId = loginRes.data.help10000_employee_id
			cmpy.EmployeeName = loginRes.data.help10000_employee_name
			cmpy.SysId = loginRes.data.sysid
			cmpy.TemplateCode = loginRes.data.help10000_passport_weixin_templatecode

			accountInfo.currCmpy = cmpy
			accountInfo.loginCompany =cmpy

			uni.setStorageSync('routeType', CmpyCatgMap[accountInfo.loginCompany.CmpyCatg]);
			uni.setStorageSync('accountInfo', accountInfo);


			console.log('loginRes>>>>>',loginRes)
			console.log('accountInfo>>>>>>>>>>',accountInfo)


			const zRes = await getZoneInfo()
			if (zRes.MsgCode == 0) {
				const provinces = []
				const zoneInfo = zRes.data.map(item => {
					provinces.push(item.name)
					item.cityList = item.citys.map(cItem => {
						cItem.townList = cItem.towns.map(tItem => {
							return tItem.name
						})
						return cItem.name
					})
					return item
				})
				uni.setStorageSync('zoneInfo', zoneInfo);
				uni.setStorageSync('provinces', provinces);
			}
		},
		getQueryVariable(variable) {
			var query = window.location.search.substring(1);
			var vars = query.split("&");
			for (var i = 0; i < vars.length; i++) {
				var pair = vars[i].split("=");
				if (pair[0] == variable) { return pair[1]; }
			}
			return (false);
		},
		getUrlParams(url) {
			// 通过 ? 分割获取后面的参数字符串
			let urlStr = url.split('?')[1]
			// 创建空对象存储参数
			let obj = {};
			// 再通过 & 将每一个参数单独分割出来
			let paramsArr = urlStr.split('&')
			for (let i = 0, len = paramsArr.length; i < len; i++) {
				// 再通过 = 将每一个参数分割为 key:value 的形式
				let arr = paramsArr[i].split('=')
				obj[arr[0]] = arr[1];
			}
			return obj
		}
	},

	onNavigationBarButtonTap(e) {
		uni.navigateTo({
			url: '/pages/my/myMessage/myMessage'
		})
	},
}
</script>

<style lang="scss"></style>
